package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param dto
     */
    @Transactional // 开启事务
    @Override
    public void add(SetmealDTO dto) {
        //1.补充基础属性，并存入setmeal（已用AOP实现了，直接传即可）
        log.info("新增套餐：{}", dto);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.add(setmeal);

        //拿到新增的套餐id
        log.info("新增套餐的id：{}", setmeal.getId());

        //2.存入setmeal_dish
        //遍历setmealDishes绑定里面的菜品id
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        //批量插入
        setmealDishMapper.addDish(setmealDishes);

    }

    /**
     * 套餐分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        //1.设置分页参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());

        //2.调用Mapper
        Page<Setmeal> page = setmealMapper.page(dto);

        //3.返回结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional // 开启事务
    @Override
    public void delete(List<Long> ids) {
        //1.判断当前状态
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.selectStatus(id);
            if (setmeal.getStatus() == 1) {
                //起售中的，不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        //2.删除
        //删除套餐
        setmealMapper.delete(ids);
        //删除套餐和菜品的关联关系
        setmealDishMapper.deleteBySetmealId(ids);

    }

    /**
     * 套餐回显
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //1.查询套餐
        SetmealVO setmealVO = setmealMapper.selectById(id);

        //2.查询套餐对应的菜品
        List<SetmealDish> setmealDishes =setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Transactional // 开启事务
    @Override
    public void update(SetmealDTO dto) {
        //1.修改套餐 setmeal表
        //补充基础属性 已用AOP实现了，直接传即可
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.update(setmeal);

        //2.修改套餐对应的菜品 setmeal_dish表
        //由于套餐里的菜品可能删除，可能修改，也可能新增。所以先全部删除，再一次性插入
        log.info("删除的套餐Id：{}", dto.getId());
        setmealDishMapper.updateBySetmealId(dto.getId());

        //遍历setmealDishes绑定里面的菜品id
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(dto.getId());
        });

        setmealDishMapper.addDish(setmealDishes);

    }

    /**
     * 套餐起售停售（复用update方法）
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        log.info("修改后的套餐内容：{}", setmeal);

        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
